class_name RigidEntity
extends RigidBody2D

##获取 entity_data
##[codeblock]
##var data = entity.get_entity_data()
##[/codeblock]
##获取 component
##[codeblock]
##var component = entity.get_component_by_name("")
##[/codeblock]
@export var entity_data:EntityData
var blackboard_data:BlackboardData
var component_dict:Dictionary
var vel_is_changed:bool = false
var vel_cache:Vector2
var angular_vel_is_changed:bool = false
var angular_vel_cache:float

func init():
	pass
func get_entity_data():
	return entity_data
func get_blackboard_data(key:String):
	return blackboard_data.get_data(key)
func set_blackboard_data(key:String,value):
	blackboard_data.set_data(key,value)
func get_blackboard_data_dict():
	return blackboard_data.get_data_dict()
func clear_blackboard_data_dict():
	blackboard_data.clear_data_dict()
func get_components() -> Array[BaseComponent]:
	var components:Array[BaseComponent] = []
	for child in get_children():
		if child is BaseComponent:
			components.append(child)
	components.sort_custom(func (a,b):return a.priority < b.priority)
	return components
func get_component_dict() -> Dictionary:
	return component_dict
func get_component_by_name(component_name:String) -> BaseComponent:
	if component_dict.has(component_name):
		return component_dict[component_name]
	return null
func set_linear_vel(vel:Vector2):
	vel_is_changed = true
	vel_cache = vel
func set_angular_vel(rad:float):
	angular_vel_is_changed = true
	angular_vel_cache = rad

func _init() -> void:
	z_index = StaticValue.Z_INDEX_ENTITY
	blackboard_data = BlackboardData.new()
	component_dict = {}
func _ready() -> void:
	for child in get_components():
		component_dict[child.component_name] = child
		child.init_component(self)
func _process(delta: float) -> void:
	pass
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
	if vel_is_changed:
		linear_velocity = vel_cache
		vel_cache = Vector2.ZERO
		vel_is_changed = false
		angular_velocity = angular_vel_cache
		angular_vel_cache = 0
		angular_vel_is_changed = false
